<?php

Raise::load('core.Rectangle');

/**
 * TestCoreRectangle class
 * Testing unit of 'phpraise/core/Rectangle.php'
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRectangle extends RaiseTest {

    public static function testRectangle(){
        self::assertClassExists('Rectangle');
        self::assertClassExtends('Rectangle', 'RaiseShape');

        $rect = new Rectangle(0 , 0 , 20, 10);
        self::assertObjectClass($rect, 'Rectangle');
        self::assertStrictEqual($rect->width(), 20);
        self::assertStrictEqual($rect->height(), 10);

        self::assertObjectClass($rect->center(), 'RaisePoint');
        // comparing objects, use == instead of ===
        self::assertEqual($rect->center(), new RaisePoint(10, 5));

        self::assertObjectClass($rect->location(), 'RaisePoint');
        // comparing objects, use == instead of ===
        self::assertEqual($rect->location(), new RaisePoint(0, 0));

        self::assertStrictEqual($rect->x(), 0);
        self::assertStrictEqual($rect->y(), 0);
        self::assertStrictEqual($rect->x2(), 20);
        self::assertStrictEqual($rect->y2(), 10);

    }
    
    public static function testRectangle2(){
        $rect = new Rectangle(new RaisePoint(102.24, 76.6), 51.6, 204.23);
        self::assertObjectClass($rect, 'Rectangle');
        self::assertStrictEqual($rect->width(), 51.6);
        self::assertStrictEqual($rect->height(), 204.23);

        // tricky part: floats with precision problem, thus converting to string in order to compare
        self::assertObjectClass($rect->center(),'RaisePoint');
        self::assertStrictEqual($rect->center()->x(), 128.04);
        self::assertEqual((string)$rect->center()->y(), (string)178.715);
        self::assertStrictEqual($rect->centerX(), 128.04);
        self::assertStrictEqual((string)$rect->centerY(), (string)178.715);

        self::assertObjectClass($rect->location(), 'RaisePoint');
        // comparing objects, use == instead of ===
        self::assertEqual($rect->location(), new RaisePoint(102.24, 76.6));

        self::assertStrictEqual($rect->x(), 102.24);
        self::assertStrictEqual($rect->y(), 76.6);
        self::assertStrictEqual($rect->x2(), 153.84);
        self::assertStrictEqual($rect->y2(), 280.83);

    }

    public static function testEquality(){
        $rect1 = new Rectangle(new RaisePoint(102.24, 76.6), 51.6, 204.23);
        $rect2 = new Rectangle(5 , 23 , 20, 10);
        $rect3 = $rect1;

        self::assertTrue($rect1->equals($rect1));
        self::assertTrue($rect1->equals($rect3));
        self::assertFalse($rect1->equals($rect2));
        self::assertFalse($rect2->equals($rect1));
    }

    public static function testToArray(){
        $rect = new Rectangle(5 , 23 , 20, 10);
        self::assertType($rect->toArray(), 'array');
        self::assertStrictEqual($rect->toArray(), array(5, 23, 20, 10));
    }

}